import React, { Component } from "react";
import Unit from "./index.js";
import "./loading.scss";
import { Pagination } from "antd";

class View extends Component {
    constructor(props) {
        super(props);
        this.state = {
            listData: [],
            loading: true,
            pPageCfg: {
                page_number: sessionStorage.getItem('currentPages') || 1,
                page_size: 10
            },
            setPageCfg: {
                currentPage: 1,
                totalPage: 10
            }
        }
        this.getPage = this.getPage.bind(this);
    }
    getPage(page, page_size = 10) {
        sessionStorage.setItem('currentPages', encodeURIComponent(page));
        const { pPageCfg } = this.state;
        pPageCfg.page_number = page;
        pPageCfg.page_size = page_size;
        Unit.getApi2('/home/mediareports', pPageCfg, {}).then((res) => {
            console.log(res.data);
            if (res.data.code === 200) {
                this.setState({
                    listData: res.data.data,
                    loading: false,
                    setPageCfg: {
                        currentPage: page,
                        totalPage: res.data.total
                    }
                })
            } else {
                //
            }
        })
    }
    lists() {
        const { listData } = this.state;
        const lists = listData.map((val, index) => {
            return (
                <li key={val.id}>
                    <a href={val.jump_url}>{val.main_title}</a>
                    <span>{val.source}</span>
                    <span>{val.sourceDate}</span>
                </li>
            )
        })
        return lists;
    }
    componentDidMount() {
        // 拼多多
        const { pPageCfg } = this.state;
        setTimeout(() => {
            this.getPage(pPageCfg.page_number);
        }, 3000)

    }
    render() {
        const { listData, loading, setPageCfg } = this.state;
        return (
            <React.Fragment>
                <ul>
                    {
                        loading
                            ?
                            (<div className="loading_box"><div className="loading"></div></div>)
                            :
                            (listData.length !== 0 ? this.lists() : <div className="no_result">小主，~什么也没有呀！</div>)
                    }
                </ul>
                {
                    listData.length !== 0 ? <Pagination onChange={this.getPage} defaultCurrent={setPageCfg.currentPage} total={setPageCfg.totalPage} /> : ''
                }
            </React.Fragment>
        )
    }

}
export default View;
